package com.sky.demo.activities;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.sky.demo.R;
import com.sky.demo.global.BaseActivity;

/**
 * @ClassName: LoginActivity
 * @Description: 登录
 * @author LiBinx
 * @date 2015年3月26日 下午4:20:17
 * 
 */
public class LoginActivity extends BaseActivity {
	private EditText et_name, et_pass;
	private Button login;
	private CheckBox check;

	private ImageView login_del_name, login_del_pass;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		initView();
		initListener();
	}

	/**
	 * 初始化控件
	 */
	public void initView() {
		et_name = (EditText) findViewById(R.id.ed_username);
		et_pass = (EditText) findViewById(R.id.ed_password);

		login = (Button) findViewById(R.id.bt_login);
		check = (CheckBox) findViewById(R.id.show_passwd);

		login_del_name = (ImageView) findViewById(R.id.login_del_name);
		login_del_pass = (ImageView) findViewById(R.id.login_del_pass);
		findViewById(R.id.forget).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
			}
		});
	}

	/**
	 * 初始化监听
	 */
	public void initListener() {
		checkTextChange(et_name,login_del_name);
		checkTextChange(et_pass,login_del_pass);
		login_del_name.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				et_name.setText(null);
			}
		});
		login_del_pass.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				et_pass.setText(null);
			}
		});
		login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String userStr = et_name.getText().toString().trim();
				String passStr = et_pass.getText().toString().trim();
				if (userStr.length() == 11 && passStr.length() > 5
						&& passStr.length() < 21) {
					login();
				} else {
					Toast.makeText(LoginActivity.this, "请检查用户名和密码后再试",
							Toast.LENGTH_SHORT).show();
				}
			}
		});
		check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					//隐藏密码
					et_pass.setTransformationMethod(HideReturnsTransformationMethod
							.getInstance());
				} else {
					//显示密码
					et_pass.setTransformationMethod(PasswordTransformationMethod
							.getInstance());
				}
			}
		});
	}

	public void checkTextChange(EditText editText, final ImageView view) {
		editText.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				if (s.length() >= 1) {
					view.setVisibility(View.VISIBLE);
				} else {
					view.setVisibility(View.INVISIBLE);
				}
			}
		});
	}

	/**
	 * 登录，成功调到主页，失败提示用户操作错误
	 */
	protected void login() {
		final String str_username = et_name.getText().toString().trim();
		String str_pass = et_pass.getText().toString().trim();
		if (str_username == null || str_username.length() <= 0) {
//			showToast("用户名不能为空");
			return;
		}
		if (str_pass == null || str_pass.length() <= 0) {
//			showToast("密码不能为空");
			return;
		}
	}


}
